package com.yh.homestay.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yh.homestay.common.GuestsCarEditResult;
import com.yh.homestay.common.HomeStayResult;
import com.yh.homestay.dao.GuestsCarLikeMapper;
import com.yh.homestay.dao.GuestsCarMapper;
import com.yh.homestay.pojo.GuestsCar;
import com.yh.homestay.pojo.GuestsCarExample;
import com.yh.homestay.service.GuestsCarService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class GuestsCarServiceImpl implements GuestsCarService {
    @Autowired
    private GuestsCarMapper guestsCarMapper;
    @Autowired
    private GuestsCarLikeMapper carMapper;

//    @Override
//    public ResultMessage selectAll() {
//        GuestsCarExample guestsCarExample = new GuestsCarExample();
//        GuestsCarExample.Criteria criteria = guestsCarExample.createCriteria();
//        List<GuestsCar> car = guestsCarMapper.selectByExample(guestsCarExample);
//        return new ResultMessage(0, "ok", car.size(), car);
//    }

    @Override
    public HomeStayResult selectAll(Integer page, Integer limit, String carType) {
        Map map=new HashMap();
        map.put("carType",carType);
        PageHelper.startPage(page,limit);
        List<GuestsCar> byName = carMapper.findByname(map);
        PageInfo pageInfo = new PageInfo(byName);
        HomeStayResult res=new HomeStayResult(0," ", (int) pageInfo.getTotal(),pageInfo.getList());
        return res;
    }

    @Override
    public HomeStayResult addCar(GuestsCar guestsCar) {
        int insert = guestsCarMapper.insert(guestsCar);
        if(insert>0){
            return new HomeStayResult(200, "ok",0,null);
        }
        return new HomeStayResult(500, "ok",0,null);
    }

    @Override
    public HomeStayResult delete(String id) {
        GuestsCar guestsCar = new GuestsCar();
        guestsCar.setCarId(id);
        int delete = guestsCarMapper.delete(guestsCar);
        if (delete > 0) {
            return new HomeStayResult(200, "ok",0,null);
        } else

            return new HomeStayResult(500, "error",0,null);
    }

    @Override
    public HomeStayResult update(String id, String carType, String carColor, Integer carHoldCount, Integer carStatus) {
        GuestsCar guestsCar = new GuestsCar();
        // guestsCar.setCarId(id);
        guestsCar.setCarColor(carColor);
        guestsCar.setCarHoldCount(carHoldCount);
        guestsCar.setCarType(carType);
        guestsCar.setCarStatus(carStatus);
        GuestsCarExample example = new GuestsCarExample();
        GuestsCarExample.Criteria criteria = example.createCriteria();
        criteria.andCarIdEqualTo(id);
        int update = guestsCarMapper.updateByExampleSelective(guestsCar, example);
        if (update > 0) {
            return new HomeStayResult(200, "ok",0,null);
        } else {
            return new HomeStayResult(500, "error",0,null);
        }
    }

    @Override
    public GuestsCarEditResult selectone(String id) {
        GuestsCarExample guestsCarExample=new GuestsCarExample();
        GuestsCarExample.Criteria criteria = guestsCarExample.createCriteria();
        criteria.andCarIdEqualTo(id);
        List<GuestsCar> guestsCars = guestsCarMapper.selectByExample(guestsCarExample);
        if(guestsCars !=null && guestsCars.size() >0){
            return  new GuestsCarEditResult(200,"ok",guestsCars.get(0));
        }else{
            return new GuestsCarEditResult(500,"error",null);
        }
    }

    @Override
    public GuestsCarEditResult selecteCarById(String id) {
        GuestsCar car = guestsCarMapper.selectByPrimaryKey(id);
        return new GuestsCarEditResult(200,"ok",car);
    }

    @Override
    public GuestsCarEditResult updateGuestsCar(GuestsCar guestsCar) {
        guestsCarMapper.updateByPrimaryKeySelective(guestsCar);
        return new GuestsCarEditResult(200,"ok",null);
    }

    @Override
    public HomeStayResult deleteCarS(List<String> ids) {
        GuestsCarExample example = new GuestsCarExample();
        GuestsCarExample.Criteria criteria = example.createCriteria();
        criteria.andCarIdIn(ids);
        int delete = guestsCarMapper.deleteByExample(example);
        if (delete > 0) {
            return new HomeStayResult(200, "ok",0,null);
        } else {
            return new HomeStayResult(500, "error",0,null);
        }
    }
}